<?php
namespace app\api\controller;
use app\api\controller\Pub;

use app\common\controller\CardLevel;
use app\common\model\Agent;
use app\common\model\CardConsume;
use app\common\model\CardGeneral;
use app\common\model\CardJcUserget;
use app\common\model\CardMember;
use app\common\model\CardQrcode;
use app\common\model\CardRank;
use app\common\model\CardUserBonus;
use app\common\model\CardUserFee;
use app\common\model\CardUserGet;
use app\common\model\CardUserMarkLog;
use app\common\model\Merchant;
use app\common\model\Paylog;
use think\Request;
use think\Session;
class CardThreeMember extends Pub
{

    //获取卡券类型
    public function getcardtype($type)
    {
        swiTch ($type) {
            case 'GROUPON':
                $rest = "团购券";
                return $rest;
                break;
            case 'CASH':
                $rest = "代金券";
                return $rest;
                break;
            case 'DISCOUNT':
                $rest = "折扣券";
                return $rest;
                break;
            case 'GIFT':
                $rest = "兑换券";
                return $rest;
                break;
            case 'GENERAL_COUPON':
                $rest = "优惠券";
                return $rest;
                break;
            case 'GENERAL_CARD':
                $rest = "礼品卡";
                return $rest;
                break;
        }

    }


    //会员信息修改
    public function member_edit($param){
        $CardUserGet = new CardUserGet();
        $CardRank = new CardRank();
//        $map['mid'] = ['in', $param['merchantid']];
        $map['openid'] = $param['openid'];
        $map['code'] = $param['code'];
        $user = $CardUserGet->where($map)->find();
        if ($user){
            $maps['card_id'] = $user['card_id'];
            $maps['id'] = $param['rank'];
            $rank = $CardRank->where($maps)->find();
            if ($rank) {
                $data['rank_name'] = $rank['live_title'];
                $data['rank'] = $rank['live'];
                //END
                $datas['level_title'] = $rank['live_title'];
                $datas['card_id'] = $user['card_id'];
                $datas['code'] = $user['code'];
                $datas['background_pic_url'] = $rank['bg_img_url'];
                $CardLevel = new CardLevel();
                $CardLevel->updatelevel($datas);
            }
            $data['name'] = $param['name'];
            $data['status'] = !empty($param['status'])?$param['status']:1;
            $data['phone'] = $param['phone'];
            $data['birthday'] = $param['birthday'];
            $data['remark'] = $param['remark'];
            $data['update_time'] = time();
            $res = $CardUserGet->allowField(true)->save($data,$map);
            if ($res){
//                if ($param['remark']){
                    $this->rec_mark($map);
//                }
                $res_data['return_code'] = "SUCCESS";
                $res_data['return_msg'] = "SUCCESS";
                $res_data['result_code'] = "SUCCESS";
                $res_data['result_msg'] = "修改成功";
            }else{
                $res_data['return_code'] = "SUCCESS";
                $res_data['return_msg'] = "SUCCESS";
                $res_data['result_code'] = "FAIL";
                $res_data['result_msg'] = "修改失败";
            }
        }else{
            $res_data['return_code'] = "SUCCESS";
            $res_data['return_msg'] = "SUCCESS";
            $res_data['result_code'] = "FAIL";
            $res_data['result_msg'] = "无此会员信息";
        }
        return $res_data;
    }

    //备注记录
    private function rec_mark($map){
        $CardUserGet = new CardUserGet();
        $user = $CardUserGet->where($map)->field('code,card_id,mid,remark,openid')->find();
        $CardUserMarkLog = new CardUserMarkLog();
        $data['code'] = $user['code'];
        $data['card_id'] = $user['card_id'];
        $data['mid'] = $user['mid'];
        $data['con_username'] = "";
        $data['remark'] = $user['remark'];
        $data['openid'] = $user['openid'];
        $data['create_time'] = time();
        $s = $CardUserMarkLog->allowField(true)->save($data);
        return $s;
    }

    //会员卡券列表
    public function member_cards_list($param)
    {
        try {
            $CardUserGet = new CardUserGet();
            $page = ($param['page'] - 1) * 10;
            $map['a.mid'] = ['in', $param['merchantid']];
            $map['a.card_type'] = ['neq','MEMBER_CARD'];
            $map['a.openid'] = $param['openid'];
            $list = $CardUserGet->alias('a')->join('wn_card_general b','a.card_id=b.card_id','left')
                ->where($map)->field('a.id,a.card_id,a.code,a.card_type,a.create_time,b.title,b.brand_name')
                ->limit($page, 10)
                ->order('a.id desc')
                ->select();
            $data['$map'] = $map;
            $data['sql'] = $CardUserGet->getLastSql();
            $total = $CardUserGet->alias('a')->where($map)->count('id');
            if ($list) {
                foreach ($list as $k => $v) {
                    $list[$k]['card_type'] = $this->getcardtype($v['card_type']);
                    $list[$k]['code'] = $v['code'];
                }
                $data['return_code'] = "SUCCESS";
                $data['return_msg'] = "SUCCESS";
                $data['result_code'] = "SUCCESS";
                $data['result_msg'] = "SUCCESS";
                $data['total'] = $total;
                $data['info'] = $list;

            } else {
                $data['return_code'] = "SUCCESS";
                $data['return_msg'] = "SUCCESS";
                $data['result_code'] = "FAIL";
                $data['result_msg'] = "此会员无卡券";
            }
        } catch (\Exception $e) {
            $data['return_code'] = "FAIL";
            $data['return_msg'] = "系统错误";
            $data['result_code'] = "FAIL";
            $data['result_msg'] = $e->getMessage();
        }
        return $data;
    }

    //会员卡券列表
    public function member_cards_info($param)
    {
        try {
            $CardUserGet = new CardUserGet();
            $page = ($param['page'] - 1) * 10;
            $map['a.mid'] = ['in', $param['merchantid']];
            $map['a.card_type'] = ['neq','MEMBER_CARD'];
            $map['a.code'] = $param['openid'];
            $list = $CardUserGet->alias('a')->join('wn_card_general b','a.card_id=b.card_id','left')
                ->where($map)->field('a.id,a.card_id,a.code,a.card_type,a.create_time,b.title,b.brand_name')
                ->find();
            $data['$map'] = $map;
            $data['sql'] = $CardUserGet->getLastSql();
            $total = $CardUserGet->alias('a')->where($map)->count('id');
            if ($list) {
                foreach ($list as $k => $v) {
                    $list[$k]['card_type'] = $this->getcardtype($v['card_type']);
                    $list[$k]['code'] = $v['code'];
                }
                $data['return_code'] = "SUCCESS";
                $data['return_msg'] = "SUCCESS";
                $data['result_code'] = "SUCCESS";
                $data['result_msg'] = "SUCCESS";
                $data['total'] = $total;
                $data['info'] = $list;

            } else {
                $data['return_code'] = "SUCCESS";
                $data['return_msg'] = "SUCCESS";
                $data['result_code'] = "FAIL";
                $data['result_msg'] = "此会员无卡券";
            }
        } catch (\Exception $e) {
            $data['return_code'] = "FAIL";
            $data['return_msg'] = "系统错误";
            $data['result_code'] = "FAIL";
            $data['result_msg'] = $e->getMessage();
        }
        return $data;
    }

    //会员次卡列表
    public function member_cardc_list($param)
    {
        try {
            $page = ($param['page'] - 1) * 10;
            $CardJcUserget = new CardJcUserget();
            $map['a.mid'] = ['in', $param['merchantid']];
//            $map['a.card_type'] = 'MEMBER_CARD';
            $map['a.openid'] = $param['openid'];
            $list = $CardJcUserget->alias('a')
                ->join('wn_card_jc_list b','a.card_id=b.card_id')->where($map)
                ->field('a.id,a.num,a.surplus_num,a.addtime,a.end_time,a.term_of,a.is_del,b.title,b.store')
                ->order('a.id desc')
                ->limit($page, 10)
                ->select();
            $total = $CardJcUserget->alias('a')->where($map)->count('id');
            if ($list) {

                $data['return_code'] = "SUCCESS";
                $data['return_msg'] = "SUCCESS";
                $data['result_code'] = "SUCCESS";
                $data['result_msg'] = "SUCCESS";
                $data['total'] = $total;
                $data['info'] = $list;

            } else {
                $data = array("return_code" => "SUCCESS", "return_msg" => "SUCCESS", "result_code" => "FAIL", "result_msg" => "此会员无卡券",);
            }
        } catch (\Exception $e) {
            $data = array("return_code" => "FAIL", "return_msg" => "系统错误", "result_code" => "FAIL", "result_msg" => $e->getMessage());
        }
        return $data;
    }


    //会员积分列表
    public function member_bonus_list($param)
    {
        try {
            $page = ($param['page'] - 1) * 10;
            $CardUserBonus = new CardUserBonus();
            $map['a.mid'] = ['in', $param['merchantid']];
            $map['a.openid'] = $param['openid'];
            $map['a.code'] = $param['code'];
            $map['a.num'] = ['neq',0];
            $list = $CardUserBonus->alias('a')
                ->join('wn_merchant b','a.mid=b.id','left')->where($map)
                ->field('a.*,b.merchant')
                ->limit($page, 10)
                ->order('a.id desc')
                ->select();
            $total = $CardUserBonus->alias('a')->where($map)->count('id');
            if ($list) {

                $data['return_code'] = "SUCCESS";
                $data['return_msg'] = "SUCCESS";
                $data['result_code'] = "SUCCESS";
                $data['result_msg'] = "SUCCESS";
                $data['total'] = $total;
                $data['info'] = $list;
            } else {
                $data = array("return_code" => "SUCCESS", "return_msg" => "SUCCESS", "result_code" => "FAIL", "result_msg" => "此会员无积分记录",);
            }
        } catch (\Exception $e) {
            $data = array("return_code" => "FAIL", "return_msg" => "系统错误", "result_code" => "FAIL", "result_msg" => $e->getMessage());
        }
        return $data;
    }

    //会员积分详情
    public function member_bonus_info($param)
    {
        try {
            $CardUserBonus = new CardUserBonus();
            $map['a.mid'] = ['in', $param['merchantid']];
//            $map['a.openid'] = $param['openid'];
//            $map['a.code'] = $param['code'];
            $map['a.id'] = $param['id'];
            $info = $CardUserBonus->alias('a')
                ->join('wn_merchant b','a.mid=b.id','left')->where($map)
                ->field('a.bus_orderid,a.code,a.orderid,a.num,a.pay_cate,a.type,a.source,a.kuantai,a.remark,a.action_user,a.create_time,b.merchant')->find();
            if ($info) {
                $data['return_code'] = "SUCCESS";
                $data['return_msg'] = "SUCCESS";
                $data['result_code'] = "SUCCESS";
                $data['result_msg'] = "SUCCESS";
                $data['info'] = $info;
            } else {
                $data = array("return_code" => "SUCCESS", "return_msg" => "SUCCESS", "result_code" => "FAIL", "result_msg" => "此积分记录异常",);
            }
        } catch (\Exception $e) {
            $data = array("return_code" => "FAIL", "return_msg" => "系统错误", "result_code" => "FAIL", "result_msg" => $e->getMessage());
        }
        return $data;
    }




    //会员余额列表
    public function member_balance_list($param)
    {
        try {
            $page = ($param['page'] - 1) * 10;
            $CardUserFee = new CardUserFee();
            $map['a.mid'] = ['in', $param['merchantid']];
            $map['a.openid'] = $param['openid'];
            $map['a.code'] = $param['code'];
            $map['a.type'] = 1;
            $map['a.num'] = ['neq',0];
            $list = $CardUserFee->alias('a')
                ->join('wn_merchant b','a.mid=b.id','left')
                ->join('wn_card_user_get c','a.code=c.code','left')
                ->where($map)->field('a.*,b.merchant,c.nickname,c.name')->limit($page, 10)->order('a.id desc')->select();
            $total = $CardUserFee->alias('a')->where($map)->count('id');
            if ($list) {
                foreach ($list as $k=>$v){
                    if ($v['pay_cate'] == 0 ) {
                        $list[$k]['remark'] = '微信' . $v['remark'];
                    }
                    $list[$k]['name'] = empty($v['name'])?$v['nickname']:$v['name'];
                }
                $data['return_code'] = "SUCCESS";
                $data['return_msg'] = "SUCCESS";
                $data['result_code'] = "SUCCESS";
                $data['result_msg'] = "SUCCESS";
                $data['total'] = $total;
                $data['info'] = $list;
            } else {
                $data = array("return_code" => "SUCCESS", "return_msg" => "SUCCESS", "result_code" => "FAIL", "result_msg" => "此会员无余额记录",);
            }
        } catch (\Exception $e) {
            $data = array("return_code" => "FAIL", "return_msg" => "系统错误", "result_code" => "FAIL", "result_msg" => $e->getMessage());
        }
        return $data;
    }

    //会员余额详情
    public function member_balance_info($param)
    {
        try {
            $CardUserBonus = new CardUserBonus();
            $map['a.mid'] = ['in', $param['merchantid']];
//            $map['a.openid'] = $param['openid'];
//            $map['a.code'] = $param['code'];
            $map['a.id'] = $param['id'];
            $info = $CardUserBonus->alias('a')
                ->join('wn_merchant b','a.mid=b.id','left')
                ->where($map)->field('a.bus_orderid,a.code,a.orderid,a.num,a.pay_cate,a.type,a.source,a.kuantai,a.remark,a.action_user,a.create_time,b.merchant')->find();
            if ($info) {
                $data['return_code'] = "SUCCESS";
                $data['return_msg'] = "SUCCESS";
                $data['result_code'] = "SUCCESS";
                $data['result_msg'] = "SUCCESS";
                $data['info'] = $info;
            } else {
                $data = array("return_code" => "SUCCESS", "return_msg" => "SUCCESS", "result_code" => "FAIL", "result_msg" => "此积分记录异常",);
            }
        } catch (\Exception $e) {
            $data = array("return_code" => "FAIL", "return_msg" => "系统错误", "result_code" => "FAIL", "result_msg" => $e->getMessage());
        }
        return $data;
    }

    //会员余额列表
    public function member_payfee_list($param)
    {
        try {
            $page = ($param['page'] - 1) * 10;
            $CardUserFee = new CardUserFee();
            if (!empty($param['search_val'])){
                $map['c.phone|a.code|c.old_code'] = array('like', '%' . $param['search_val'] . '%');
            }
            $map['a.mid'] = ['in', $param['merchantid']];
//            $map['a.openid'] = $param['openid'];
//            $map['a.code'] = $param['code'];
            $map['a.type'] = $param['type'];
            $map['a.num'] = ['neq',0];
            $list = $CardUserFee->alias('a')
                ->join('wn_merchant b','a.mid=b.id','left')
                ->join('wn_card_user_get c','a.code=c.code','right')
                ->where($map)->field('a.*,b.merchant,c.nickname,c.name')->limit($page, 10)->select();
            $total = $CardUserFee->alias('a')->join('wn_card_user_get c','a.code=c.code','right')->where($map)->order('a.id desc')->count();
            if ($list) {
                foreach ($list as $k=>$v){
                    if ($v['pay_cate'] == 0 ) {
                        $list[$k]['remark'] = '微信' . $v['remark'];
                    }
                    $list[$k]['name'] = empty($v['name'])?$v['nickname']:$v['name'];
                }
                $data['return_code'] = "SUCCESS";
                $data['return_msg'] = "SUCCESS";
                $data['result_code'] = "SUCCESS";
                $data['result_msg'] = "SUCCESS";
                $data['total'] = $total;
                $data['info'] = $list;
            } else {
                $data = array("return_code" => "SUCCESS", "return_msg" => "SUCCESS", "result_code" => "FAIL", "result_msg" => "无余额记录",);
            }
        } catch (\Exception $e) {
            $data = array("return_code" => "FAIL", "return_msg" => "系统错误", "result_code" => "FAIL", "result_msg" => $e->getMessage());
        }
        return $data;
    }


    //会员消费
    public function member_pay_list($param){
        $page=($param['page']-1)*10;
//        $map['a.openid'] = $param['openid'];
//        $map['a.pay_statu'] = 1;
//        $map['a.pay_fs'] = ['in',[0,2,8]];
//        $map['a.pay_cate'] = ['neq','recharge'];
        $map['mid'] = ['in',$param['merchantid']];
        $map['openid'] = $param['openid'];
        $map['pay_statu'] = 1;
        $map['pay_fs'] = ['in',[0,2,8]];
        $map['pay_cate'] = ['neq','recharge'];
//        $map['b.card_type'] = "MEMBER_CARD";
        $list = [];
        $Paylog = new Paylog();
        $list = $Paylog
//            ->alias('a')
//            ->join('wn_card_user_get b','a.openid=b.openid','left')   ,b.code,b.name,b.nickname
            ->where($map)
            ->limit($page,10)
//            ->field('a.mid,a.pay_cate,a.pay_fs,a.pay_statu,a.price,a.add_time,a.pay_time,a.d_cashier')
            ->field('id,mid,pay_cate,pay_fs,pay_statu,price,add_time,pay_time,d_cashier')
            ->order('add_time','desc')
            ->limit($page, 10)->order('id desc')->select();
        $total = $Paylog
//            ->alias('a')
//            ->join('wn_card_user_get b','a.openid=b.openid','left')
            ->where($map)->count();
        foreach ($list as $k=>$v){
//            if (!empty($v['code'])) {
                $list[$k] = $v;
                $list[$k]['pay_fs'] = getpayfs($v['pay_fs']);
                $list[$k]['merchant'] = getmachname($v['mid']);
                $list[$k]['add_time'] = date('Y-m-d h:i:s', $v['add_time']);
                $list[$k]['pay_statu'] = getpaystatus($v['pay_statu']);
//                $list[$k]['name'] = empty($v['name']) ? $v['nickname'] : $v['name'];
//            }
        }
        if ($list){
            $data['return_code'] = "SUCCESS";
            $data['return_msg'] = "SUCCESS";
            $data['result_code'] = "SUCCESS";
            $data['result_msg'] = "SUCCESS";
            $data['total'] = $total;
            $data['info'] = $list;

        }else{
            $data['return_code'] = "SUCCESS";
            $data['return_msg'] = "SUCCESS";
            $data['result_code'] = "SUCCESS";
            $data['result_msg'] = "SUCCESS";
            $data['info'] = [];
        }
        return $data;
    }

    public function cards_consume_info($param){
        $CardConsume = new CardConsume();
        $map['a.mid'] = $param['merchantid'];
        $map['a.code'] = $param['search_val'];
        $info = $CardConsume->alias('a')
            ->join('wn_merchant b','a.card_id=b.card_id','left')
            ->join('wn_card_general c','a.card_id=c.card_id','left')
            ->field('a.card_id,a.code,a.mid,a.con_usersume,a.hx_fs,a.create_time,b.merchant,c.card_type,c.title,c.least_cost,c.reduce_cost')
            ->where($map)->find();
        $data = [];
        if ($info){
            $data['code'] = $info['code'];
            $data['card_type'] = getcardtype($info['card_type']);
            $data['con_time'] = $info['create_time'];
            $data['con_username'] = $info['con_username'];
            $data['con_type'] = $info['hx_fs'];
            $data['card_name'] = ['card_type'] =='CASH'?'满'.$info['least_cost'].'减'.$info['reduce_cost'].'代金券':$info['title'];;
        }
        if ($data) {
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = '';
            $rarr['result_code'] = 'SUCCESS';
            $rarr['result_msg'] = '';
            $rarr['info'] = $data;

        } else {
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = '';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '获取失败';
            $rarr['info'] = [];
        }
        return $rarr;
    }

    //会员详情
    public function member_info($param){
        $mids = $this->getmidlist('mid', $param['merchantid']);
        $mid = implode(',', $mids);
        $map['a.mid'] = array('in', $mid);
        $map['a.id'] = $param['id'];
        $CardUserGet = new CardUserGet();
        $info = $CardUserGet->alias('a')
            ->join('wn_card_fans f', 'f.openid=a.openid', 'left')
            ->where($map)->field('a.id,a.bonus,a.balance,a.name,a.rank,a.phone,a.sex,a.code,a.create_time,f.head_url,f.nickname')->find();



    }

}
